package com.xiaoyu.izf.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.xiaoyu.izf.annotation.RequestAuth;
import com.xiaoyu.izf.model.MerchantPayChannel;
import com.xiaoyu.izf.model.MerchantWxPayChannelInfo;
import com.xiaoyu.izf.model.MerchantZfbPayChannelInfo;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.FormResultBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.service.MerchantPayChannelService;
import com.xiaoyu.izf.service.MerchantWxPayChannelInfoService;
import com.xiaoyu.izf.service.MerchantZfbPayChannelInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 支付通道
 */
@RestController
@RequestMapping("/pay_channel")
public class MerchantPayChannelController extends BaseController{

    @Autowired
    private MerchantPayChannelService merchantPayChannelService;
    @Autowired
    private MerchantWxPayChannelInfoService merchantWxPayChannelInfoService;
    @Autowired
    private MerchantZfbPayChannelInfoService merchantZfbPayChannelInfoService;

    @RequestMapping("/list")
//    @RequestAuth
    public DataTableBean payChannelList(@RequestBody String body){
        System.out.println(body+"================");
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = merchantPayChannelService.listByMerchantId(paramListBean);
        return dataTableBean;
    }

    /**
     * 申请开通支付宝
     * @param request
     * @param payChannelId
     * @param zi
     * @return
     */
    @RequestMapping("/apply_zfb")
    @ResponseBody
    @RequestAuth()
    public FormResultBean applyOpenZfb(HttpServletRequest request, int payChannelId, MerchantZfbPayChannelInfo zi) {
        FormResultBean fr = new FormResultBean();

        MerchantPayChannel payChannel = merchantPayChannelService.findOneById(payChannelId);
        if (payChannel==null){
            return fr.setType("error").setMessage("支付方式不存在。");
        }

        if (zi.getSettleType()==2){
            if (!Validator.isMobile(zi.getSettlemanPhone())){
                return fr.setType("error").setMessage("手机号格式错误。");
            }
            if (!Validator.isCitizenId(zi.getSettlemanIdcardnumber())){
                return fr.setType("error").setMessage("身份证号码格式不正确。");
            }
        }

        if (StrUtil.isBlank(zi.getPicBankcard())||StrUtil.isBlank(zi.getPicBusinessLicence())||StrUtil.isBlank(zi.getPicCashier())||
                StrUtil.isBlank(zi.getPicDoorhead())||StrUtil.isBlank(zi.getPicIdcardInhand())||StrUtil.isBlank(zi.getPicLeaseContract())||
                StrUtil.isBlank(zi.getPicLegalpersonIdcardFront())||StrUtil.isBlank(zi.getPicLegalpersonIdcardReverse())||StrUtil.isBlank(zi.getPicManageLocation())||
                StrUtil.isBlank(zi.getPicOpenAccountLicence())||
                StrUtil.isBlank(zi.getPicSettlemanIdcardFront())||StrUtil.isBlank(zi.getPicSettlemanIdcardReverse())||StrUtil.isBlank(zi.getPicUnlegalpersonPrivateAuthletter())){
            return fr.setType("error").setMessage("请上传对应资质图片。");
        }

        merchantZfbPayChannelInfoService.save(payChannel,request);

        return fr.setType("success").setMessage("操作成功。");
    }

    /**
     * 申请开通微信
     * @param request
     * @param payChannelId
     * @param wi
     * @return
     */
    @RequestMapping("/apply_wx")
    @ResponseBody
    @RequestAuth()
    public FormResultBean applyOpenWx(HttpServletRequest request, int payChannelId, MerchantWxPayChannelInfo wi) {
        FormResultBean fr = new FormResultBean();

        MerchantPayChannel payChannel = merchantPayChannelService.findOneById(payChannelId);
        if (payChannel==null){
            return fr.setType("error").setMessage("支付通道不存在。");
        }

        if (wi.getSettleType()==2){
            if (!Validator.isMobile(wi.getSettlemanPhone())){
                return fr.setType("error").setMessage("手机号格式错误。");
            }
            if (!Validator.isCitizenId(wi.getSettlemanIdcardnumber())){
                return fr.setType("error").setMessage("身份证号码格式不正确。");
            }
        }

        if (StrUtil.isBlank(wi.getPicBankcard())||StrUtil.isBlank(wi.getPicBusinessLicence())||StrUtil.isBlank(wi.getPicCashier())||
                StrUtil.isBlank(wi.getPicDoorhead())||StrUtil.isBlank(wi.getPicIdcardInhand())||StrUtil.isBlank(wi.getPicLeaseContract())||
                StrUtil.isBlank(wi.getPicLegalpersonIdcardFront())||StrUtil.isBlank(wi.getPicLegalpersonIdcardReverse())||StrUtil.isBlank(wi.getPicManageLocation())||
                StrUtil.isBlank(wi.getPicOpenAccountLicence())||
                StrUtil.isBlank(wi.getPicSettlemanIdcardFront())||StrUtil.isBlank(wi.getPicSettlemanIdcardReverse())||StrUtil.isBlank(wi.getPicUnlegalpersonPrivateAuthletter())){
            return fr.setType("error").setMessage("请上传对应资质图片。");
        }

        merchantWxPayChannelInfoService.save(payChannel,request);

        return fr.setType("success").setMessage("操作成功。");
    }
}
